package com.moxiao.yfjh.module.blind.service.mechanism;

import com.moxiao.yfjh.module.blind.controller.admin.mechanism.vo.MemberMechanismCreateReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.mechanism.vo.MemberMechanismExportReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.mechanism.vo.MemberMechanismPageReqVO;
import com.moxiao.yfjh.module.blind.controller.admin.mechanism.vo.MemberMechanismUpdateReqVO;
import com.moxiao.yfjh.module.blind.convert.mechanism.MemberMechanismConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.mechanism.MemberMechanismDO;
import com.moxiao.yfjh.module.blind.dal.mysql.mechanism.MemberMechanismMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.blind.controller.admin.mechanism.vo.*;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.blind.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲会员等级机制 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MemberMechanismServiceImpl implements MemberMechanismService {

    @Resource
    private MemberMechanismMapper memberMechanismMapper;

    @Override
    public void createMemberMechanism(List<MemberMechanismCreateReqVO> createReqVO) {
        // 插入
        List<MemberMechanismDO> convert = MemberMechanismConvert.INSTANCE.convert(createReqVO);
        Collection<MemberMechanismDO> memberMechanisms= new ArrayList<>(convert);
        memberMechanismMapper.insertBatch(memberMechanisms);
    }

    @Override
    public void updateMemberMechanism(List<MemberMechanismUpdateReqVO> updateReqVO) {
        updateReqVO.forEach(memberMechanismUpdateReqVO -> {
            // 校验存在
            validateMemberMechanismExists(memberMechanismUpdateReqVO.getId());
            // 更新
            MemberMechanismDO updateObj = MemberMechanismConvert.INSTANCE.convert(memberMechanismUpdateReqVO);
            memberMechanismMapper.updateById(updateObj);
        });

    }

    @Override
    public void deleteMemberMechanism(Long id) {
        // 校验存在
        validateMemberMechanismExists(id);
        // 删除
        memberMechanismMapper.deleteById(id);
    }

    private void validateMemberMechanismExists(Long id) {
        if (memberMechanismMapper.selectById(id) == null) {
            throw exception(MEMBER_MECHANISM_NOT_EXISTS);
        }
    }

    @Override
    public MemberMechanismDO getMemberMechanism(Long id) {
        return memberMechanismMapper.selectById(id);
    }

    @Override
    public List<MemberMechanismDO> getMemberMechanismList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return memberMechanismMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberMechanismDO> getMemberMechanismPage(MemberMechanismPageReqVO pageReqVO) {
        return memberMechanismMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberMechanismDO> getMemberMechanismList(MemberMechanismExportReqVO exportReqVO) {
        return memberMechanismMapper.selectList(exportReqVO);
    }

}
